cmake_minimum_required(VERSION 3.15)

project(peer_connection)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set (PREFIX /usr/local/mdc)
if(APPLE)
    set(LIBWEBRTC_INCLUDE_PATH "/Users/malei/WorkSpace/webrtc/webrtc_m94/webrtc-checkout/src" CACHE PATH "webrtc include")
    set(LIBWEBRTC_BINARY_PATH "/Users/malei/WorkSpace/webrtc/webrtc_m94/webrtc-checkout/src/out/Release/obj" CACHE PATH "webrtc binary")
elseif(WIN32)
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        set(LIBWEBRTC_BINARY_PATH "c:/webrtc_3/webrtc_checkout/src/out/x64/Release/obj" CACHE PATH "webrtc binary")
    else()
        set(LIBWEBRTC_BINARY_PATH "c:/webrtc_3/webrtc_checkout/src/out/x86/Release1/obj" CACHE PATH "webrtc binary")
    endif()
    set(LIBWEBRTC_INCLUDE_PATH "c:/webrtc_3/webrtc_checkout/src" CACHE PATH "webrtc include")
else()
    set(LIBWEBRTC_INCLUDE_PATH ${PREFIX}/include/libwebrtc/include CACHE PATH "webrtc include")
    set(LIBWEBRTC_BINARY_PATH ${PREFIX}/lib CACHE PATH "webrtc binary")
endif()


if (UNIX)
    add_definitions(-DWEBRTC_POSIX)
    add_definitions(-DWEBRTC_EXTERNAL_JSON)
endif ()

if(APPLE)
    add_definitions(-DWEBRTC_MAC)
endif()


set(server_source
        server/data_socket.cc
        server/data_socket.h
        server/main.cc
        server/peer_channel.h
        server/peer_channel.cc
        server/utils.cc
        server/utils.h
        )


add_executable(server ${server_source})


set(client_source
        client/conductor.h
        client/conductor.cc
        client/defaults.h
        client/defaults.cc
        client/flag_defs.h
        client/field_trial.h
        client/field_trial.cc
        client/peer_connection_client.cc
        client/peer_connection_client.h
        client/linux/main_wnd.h
        client/linux/main_wnd.cc
        client/linux/main.cc
        client/capture/video_capture.cpp
        client/capture/video_capture.h
        client/capture/vcm_capture.cpp
        client/capture/vcm_capture.h
        client/capture/external_video_capturer.cpp
        client/capture/external_video_capturer.h
        client/capture/external_video_capturer_module.h
        client/capture/external_video_capturer_module.cpp)


add_executable(client ${client_source})

target_include_directories(server PUBLIC ${LIBWEBRTC_INCLUDE_PATH})
target_include_directories(server PUBLIC ${LIBWEBRTC_INCLUDE_PATH}/third_party/abseil-cpp)
target_include_directories(client PUBLIC ${LIBWEBRTC_INCLUDE_PATH}/third_party/abseil-cpp)
target_include_directories(client PUBLIC ${LIBWEBRTC_INCLUDE_PATH})
target_include_directories(client PUBLIC ${LIBWEBRTC_INCLUDE_PATH}/third_party/libyuv/include/)
target_include_directories(client PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/capture)

if(UNIX)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -fpermissive -g -fPIC")
endif(UNIX)

if(APPLE)
    find_library(APPLICATION_SERVICES ApplicationServices)
    find_library(AUDIO_TOOLBOX AudioToolbox)
    find_library(AV_FOUNDATION AVFoundation)
    find_library(CORE_AUDIO CoreAudio)
    find_library(CORE_FOUNDATION Foundation)
    find_library(CORE_MEDIA CoreMedia)
    find_library(CORE_VIDEO CoreVideo)
    find_library(CORE_SERVICES CoreServices)
set(platform_deps
        ${APPLICATION_SERVICES}
        ${AUDIO_TOOLBOX}
        ${AV_FOUNDATION}
        ${CORE_AUDIO}
        ${CORE_FOUNDATION}
        ${CORE_MEDIA}
        ${CORE_VIDEO}
        ${CORE_SERVICES})
else()
    set(platform_deps dl)
endif()
target_link_libraries(client
        ${LIBWEBRTC_BINARY_PATH}/libwebrtc${CMAKE_STATIC_LIBRARY_SUFFIX}
        pthread
        ${platform_deps}
        )

target_link_libraries(server
        ${LIBWEBRTC_BINARY_PATH}/libwebrtc${CMAKE_STATIC_LIBRARY_SUFFIX}
        pthread
        dl
        )



#### TRTC

set(tx_source
        trtc/peerconnection.cpp
        trtc/peerconnection.h
        trtc/main.cpp)

add_executable(tx_pull ${tx_source})

target_include_directories(tx_pull PUBLIC ${LIBWEBRTC_INCLUDE_PATH})
target_include_directories(tx_pull PUBLIC ${LIBWEBRTC_INCLUDE_PATH}/third_party/abseil-cpp)
target_link_libraries(tx_pull
        ${LIBWEBRTC_BINARY_PATH}/libwebrtc${CMAKE_STATIC_LIBRARY_SUFFIX}
        pthread
        ${platform_deps}
        )
